jpackage("twc.expiringModal", function(){
	var ExpiringState = Backbone.Model.extend({
		url : twc.routes.get('data/api_card_expiring'),
		defaults : {
			obpEnabled : true,
			subaccount_user : false,
			cardExpiring : true,
			payXpressUrl : '',
			paymentDueDate : ''
		}
	});
	var ExpiringView = Backbone.View.extend({
		el : '#expiring-card-modal',
		events : {
			'click a.expired-billing-app' : "showBillingApp"
		},
		showBillingHash : '#show-billing-app',
		template : 'expiring-modal-template',
		accountLink : 'data-account-url',
		subaccountAttr : 'data-subaccount',
		hawaiiAttr : 'data-hawaii-division',
		shownCookie : 'expiring-modal-shown',
		actionButtonClass : 'expired-billing-app',
		initialize : function() {
			this.$el = $(this.el);
			var subaccount = this.$el.attr(this.subaccountAttr);

			this.model = new ExpiringState();
			this.model.on('change', this.render, this);

			if (subaccount !== 'true') {
				this.model.fetch({reset:true});
			}
		},
		getUrl : function() {
			var accountLink = this.$el.attr(this.accountLink);

			var url = '';

			if (this.model.get('payXpressUrl') && (!this.model.get('obpEnabled') || this.model.get('obpEnabled') === 'false')) {
				url = this.model.get('payXpressUrl');
			} else if (this.model.get('obpEnabled')) {
				url = accountLink + this.showBillingHash;
			}
			return url;
		},
		getButtonClass : function() {
			if (this.model.get('obpEnabled')) {
				return this.actionButtonClass;
			} else {
				return '';
			}
		},
		render : function() {
            var template = twc.utils.template(this.template);
			var hasCookie = twc.utils.CookieUtil.read(this.shownCookie);
			var html = template({
				'paymentDueDate' : this.model.get('paymentDueDate'),
				'url' : this.getUrl(),
				'buttonClass' : this.getButtonClass()
			});

			this.$el.append(html);
			
			if (this.model.get('cardExpiring') && !hasCookie && !this.model.get('subaccount_user')) {
				this.$el.modal('show');
				twc.utils.CookieUtil.write(this.shownCookie, '1')
			}
		},
		showBillingApp : function(e) {
			if (window.accountBilling) {
				e.preventDefault();
				window.location.hash = "";
				window.accountBilling.showBillingType();
				this.$el.modal('hide');
				return false;
			}
		}
	});
	var test = new ExpiringView();
});


